<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Class Menu extends MY_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->checkAdmin($this->session->userdata('admin_id'));
        $this->data['controller'] = 'menu';         
        $this->load->model('Mmenu');
    }
    
    
    public function index()
    {
        $this->data['data'] = $this->Mmenu->getMenuDB($this->data['baseurl']);
        $this->data['action'] = 'index';
        $this->load->view('temp/layout', $this->data);
    }
    
    public function edit()
    {
        
        $this->load->helper('form');
        $this->load->library('form_validation');
        $this->form_validation->set_rules('nameMenu', 'Tên Menu', 'required');
        $this->form_validation->set_rules('link_all', 'Link liên kết', 'required');
        
        $this->data['id'] = $this->getParamInt('id');
        $this->data['data'] = $this->Mmenu->getDBID($this->data['id']);
        $this->data['typelink'] = $this->Mmenu->getTypeLink($this->data['data']->id_type);
        $this->data['parent'] = $this->Mmenu->getParent($this->data['data']->parent);
        
        if($this->form_validation->run() === TRUE)
        {
            $dataimage = $this->getParamString('img_hidden');
            if(!in_array($dataimage, array(null, '', '0')))
            {
                $timenow = time().rand(0,9999);
                $nameimagess = 'nhatnv-'.$timenow.'.png';
                $paths = PROJECT_PATH.'/public/upload/images/';
                $exp = explode("[removed]",$dataimage);
                $this->savefile($exp[1],$paths,$nameimagess);
                $img = 'public/upload/images/'.$nameimagess;
            }else{
                $img = '';
            }
        
            $run = $this->Mmenu->editDb($this->data['id'],$img,$this->data['baseurl']);
            if($run == 200)
            {
                die('<meta charset="utf-8"><script>alert("Sửa danh mục thành công");window.location.href="'.$this->data['baseurl'].'manage/menu.html";</script>');
            }
        }
        $this->data['action'] = 'edit';
        $this->load->view('temp/layout', $this->data);
    }
    
    public function add()
    {
        
        $this->load->helper('form');
        $this->load->library('form_validation');
        $this->form_validation->set_rules('nameMenu', 'Tên Menu', 'required');
        $this->form_validation->set_rules('link_all', 'Link liên kết', 'required');
        $this->data['typelink'] = $this->Mmenu->getTypeLink('');
        $this->data['parent'] = $this->Mmenu->getParent('');
        
        if($this->form_validation->run() === TRUE)
        {
            $dataimage = $this->getParamString('img_hidden');
            if(!in_array($dataimage, array(null, '', '0')))
            {
                $timenow = time().rand(0,9999);
                $nameimagess = 'nhatnv-'.$timenow.'.png';
                $paths = PROJECT_PATH.'/public/upload/images/';
                $exp = explode("[removed]",$dataimage);
                $this->savefile($exp[1],$paths,$nameimagess);
                $img = 'public/upload/images/'.$nameimagess;
            }else{
                $img = '';
            }
        
            $run = $this->Mmenu->addDb($img,$this->data['baseurl']);
            if($run == 200)
            {
                die('<meta charset="utf-8"><script>alert("Thêm menu thành công");window.location.href="'.$this->data['baseurl'].'manage/menu.html";</script>');
            }
        }
        $this->data['action'] = 'add';
        $this->load->view('temp/layout', $this->data);
    }
    
    public function delete()
    {
        $run = $this->Mmenu->deleteDB();
        if($run == 200)
        {
            die('<meta charset="utf-8"><script>alert("Xóa menu thành công");window.location.href="'.$this->data['baseurl'].'manage/menu.html";</script>');
        }
        
    }
    
    public function getalllink()
    {
        echo $this->Mmenu->getalllinkDB();
    }
    
    public function sortcate()
    {
        $this->data['data'] = $this->Mcategories->getNamecate();
        $this->data['action'] = 'sortcate';
        $this->load->view('temp/layout', $this->data);
    } 
    
    public function savesort()
    {
        $id_list = $this->getParamArray('id');
        $this->Mcategories->sortsaveDB($id_list);
    }
    
}
